package com.lefevre.cms.service.follow.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lefevre.cms.bean.QueryResult;
import com.lefevre.cms.bean.follow.Follow;
import com.lefevre.cms.bean.follow.Follower;
import com.lefevre.cms.mapper.FollowMapper;
import com.lefevre.cms.mapper.FollowerMapper;
import com.lefevre.cms.service.follow.FollowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 关注
 *
 * @author Lefevre
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Resource
    private FollowerMapper followerMapper;

    /**
     * 根据Id查询关注
     *
     * @param followId 关注Id
     * @return
     */
    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Follow findById(String followId) {
        return baseMapper.selectById(followId);
    }

    /**
     * 根据用户名称查询所有关注
     *
     * @param userId   用户Id
     * @param userName 用户名称
     * @return
     */
    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public List<Follow> findAllFollow(Long userId, String userName) {
        return baseMapper.selectList(new LambdaQueryWrapper<Follow>().eq(Follow::getUserName, userName));
    }

    /**
     * 根据用户名称查询关注分页
     *
     * @param userId     用户Id
     * @param userName   用户名称
     * @param firstIndex 索引开始,即从哪条记录开始
     * @param maxResult  获取多少条数据
     * @return
     */
    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<Follow> findFollowByUserName(Long userId, String userName, int firstIndex, int maxResult) {
        QueryResult<Follow> qr = new QueryResult<>();

        LambdaQueryWrapper<Follow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follow::getUserName, userName);
        wrapper.orderByDesc(Follow::getAddtime);
        Page<Follow> followPage = baseMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        qr.setResultlist(followPage.getRecords());
        qr.setTotalrecord(followPage.getTotal());
        return qr;
    }

    /**
     * 根据用户名称查询粉丝分页
     *
     * @param userId     用户Id
     * @param userName   用户名称
     * @param firstIndex 索引开始,即从哪条记录开始
     * @param maxResult  获取多少条数据
     * @return
     */
    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<Follower> findFollowerByUserName(Long userId, String userName, int firstIndex, int maxResult) {
        QueryResult<Follower> qr = new QueryResult<>();

        LambdaQueryWrapper<Follower> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follower::getUserName, userName);
        wrapper.orderByDesc(Follower::getAddtime);
        Page<Follower> followerPage = followerMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        qr.setResultlist(followerPage.getRecords());
        qr.setTotalrecord(followerPage.getTotal());
        return qr;
    }


    /**
     * 保存关注
     *
     * @param follow   关注
     * @param follower 粉丝
     */
    @Override
    public void saveFollow(Object follow, Object follower) {
        baseMapper.insert((Follow) follow);
        followerMapper.insert((Follower) follower);
    }

    /**
     * 删除关注
     *
     * @param followId   关注Id
     * @param followerId 粉丝Id
     */
    @Override
    public Integer deleteFollow(String followId, String followerId) {
        int i = baseMapper.deleteById(followId);
        i += followerMapper.deleteById(followerId);
        return i;
    }

    /**
     * 根据用户名称删除关注
     *
     * @param userNameList 用户名称集合
     */
    @Override
    public Integer deleteFollowByUserName(List<String> userNameList) {
        int j = baseMapper.delete(new LambdaUpdateWrapper<Follow>().in(Follow::getUserName, userNameList));
        j += followerMapper.delete(new LambdaUpdateWrapper<Follower>().in(Follower::getUserName, userNameList));
        return j;
    }


    /**
     * 根据用户名称查询粉丝数量
     *
     * @param userId   用户Id
     * @param userName 用户名称
     * @return
     */
    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Long findFollowerCountByUserName(Long userId, String userName) {
        return followerMapper.selectCount(new LambdaQueryWrapper<Follower>().eq(Follower::getUserName, userName));
    }

    /**
     * 根据用户名称查询关注数量
     *
     * @param userId   用户Id
     * @param userName 用户名称
     * @return
     */
    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Long findFollowCountByUserName(Long userId, String userName) {
        return baseMapper.selectCount(new LambdaQueryWrapper<Follow>().eq(Follow::getUserName, userName));
    }
}
